﻿using Framework.AspNetCore;
using Framework.AspNetCore.Extensons;
using Framework.Core.Options;
using Volo.Abp;
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
using Volo.Abp.Swashbuckle;
using my.abp.Domain.Shared;
using Framework.AuditLogging;
using my.abp.Domain.Authorization;
using my.abp.Application;
using my.abp.SqlSugarCore;
using Framework.AspNetCore.Middlewares;
using Framework.AspNetCore.UnifyResult;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Framework.AspNetCore.Mvc;

namespace my.abp
{
	[DependsOn(
		typeof(MyAbpSqlSugarCoreModule),			// SqlSugar模块
		typeof(MyAbpApplicationModule),						// 集成 应用层 模块
		typeof(AbpAspNetCoreMvcModule),						// 集成 ASP.NET Core MVC 模块
		typeof(AbpAutofacModule),							// 集成 Autofac 依赖注入框架 模块
		typeof(AbpSwashbuckleModule),						// 集成 Swagger 模块
		typeof(AbpAspNetCoreSerilogModule),					// 集成 Serilog 日志组件 模块
		typeof(AbpAspNetCoreAuthenticationJwtBearerModule), // 集成 处理基于 JWT（JSON Web Token） 的身份验证 模块
		typeof(FrameworkAspNetCoreModule),                  // 集成 Asp.Net Core 相关模块
		typeof(FrameworkAuditLoggingModule)                 // 集成 审计和审计日志记录功能 相关模块
	)]
	public class MyAbpModule : AbpModule
	{
		private const string DefaultCorsPolicyName = "Default";

		public override Task ConfigureServicesAsync(ServiceConfigurationContext context)
		{
			var configuration = context.Services.GetConfiguration();
			var host = context.Services.GetHostingEnvironment();
			var service = context.Services;

			// 动态Api
			Configure<AbpAspNetCoreMvcOptions>(options =>
			{
				options.ConventionalControllers.Create(typeof(MyAbpModule).Assembly, options => options.RemoteServiceName = "default");
				options.ConventionalControllers.Create(typeof(MyAbpApplicationModule).Assembly, options => options.RemoteServiceName = "my.abp");
			});

			service.AddHttpClient();

			// Swagger
			service.AddSwaggerGen<MyAbpModule>();

			// 采用furion格式的规范化api
			service.AddFurionUnifyResultApi();

			//配置错误处理显示详情
			Configure<AbpExceptionHandlingOptions>(options => { options.SendExceptionsDetailsToClients = true; });

			// 验证码
			service.AddCaptcha(configuration, null);

			// 跨域
			service.AddCorsAccessor(DefaultCorsPolicyName);

			// 速率限制
			service.AddRateLimiter();

			// jwt鉴权
			service.AddJwt();

			// 授权
			service.AddAuthorization();

			return Task.CompletedTask;
		}

		public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
		{
			var app = context.GetApplicationBuilder();

			app.UseRouting();

			// 跨域
			app.UseCors(DefaultCorsPolicyName);

			// 速率限制
			app.UseRateLimiter();

			//无感token，先刷新再鉴权
			app.UseMiddleware<RefreshTokenMiddleware>();

			// swagger
			app.UseAbpSwagger();

			// 请求处理 
			app.UseMiddleware<ApiInfoMiddleware>();

			//静态资源
			app.UseStaticFiles("/api/app/wwwroot");
			app.UseDefaultFiles();
			app.UseDirectoryBrowser("/api/app/wwwroot");

			// 日志记录
			app.UseAbpSerilogEnrichers();

			// 审计日志
			app.UseAuditing();

			// 鉴权
			app.UseAuthentication();

			// 动态声明中间件,在 UseAuthorization 之前添加此行
			app.UseDynamicClaims();

			// 授权
			app.UseAuthorization();

			// 终节点
			app.UseConfiguredEndpoints();

			return Task.CompletedTask;
		}
	}
}
